﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_DLL.Product;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class Frm_OptionDataEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsOptionDataTable obj = new ClsOptionDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        public EventHandler Re_Load;

        public string ID;
        public string InputDate;
        public string PartID;
        public string MachineID;
        public string OptionID;
        public string LastOption;
        public string JigID;
        public string ToolQty;
        public string ProTime;
        public string ClampTime;
        public string TechDate;
        public string UpdateDay;
        public string StaffID;
        public string Note;
        public string AondNote;
        public string Memo;
        public string CLUpdateday;
        public string Jig;
        public string JigType;

        public Frm_OptionDataEdit()
        {
            InitializeComponent();
        }

        private void Frm_OptionDataEdit_Load(object sender, EventArgs e)
        {
            //lấy PartID  PartID

            dt = clspartDAO.GetAllPartID();
            DataRow r1 = dt.NewRow();
            r1["PartNo"] = "";
            r1["PartNo"] = "";
            dt.Rows.InsertAt(r1, 0);
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";

            // lấy StaffID
            DataTable dt1;
            dt1 = clspartDAO.GetAllStaffID();
            DataRow r = dt1.NewRow();
            r["FullName"] = "";
            r["StaffID"] = "";
            dt1.Rows.InsertAt(r, 0);
            cbStaffID.DataSource = dt1;
            cbStaffID.DisplayMember = "FullName";
            cbStaffID.ValueMember = "StaffID";

            // lấy OptionID
            DataTable dt2;
            dt2 = clspartDAO.GetAllOptionID();
            DataRow r2 = dt2.NewRow();
            r2["OptionID"] = "";
            r2["OptionID"] = "";
            dt2.Rows.InsertAt(r2, 0);
            cbOptionID.DataSource = dt2;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            DataTable dt3;
            dt3 = clspartDAO.GetAllMachineID();
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachineID.DataSource = dt3;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //Lấy jigID
            DataTable dt4;
            dt4 = clspartDAO.GetAllJigID();
            DataRow r4 = dt4.NewRow();
            r4["JigName"] = "";
            //r4["JigType"] = "";
            dt4.Rows.InsertAt(r4, 0);
            cbJigID.DataSource = dt4;
            cbJigID.DisplayMember = "JigName";
            //cbJigID.ValueMember = "JigType";
            
            //điền giá trị lên các trường
            cbJigID.SelectedText = JigID;
            cbMachineID.SelectedValue = MachineID;
            cbOptionID.SelectedValue = OptionID;
            cbPartID.SelectedValue = PartID;
            cbStaffID.SelectedText = StaffID;
            txtAondNote.Text = AondNote;
            txtClampTime.Text = ClampTime;
            txtMemo.Text = Memo;
            txtNote.Text = Note;
            txtProTime.Text = ProTime;
            txtToolQty.Text = ToolQty;

            if (CLUpdateday == "")
            {
                dateCLUpdateday.DateTime = DateTime.Now;

            }
            else
            {

                dateCLUpdateday.DateTime = DateTime.Parse(CLUpdateday);
            }
            if (UpdateDay == "")
            {
                dateUpdateDay.DateTime = DateTime.Now;
            }
            else
            {
                dateUpdateDay.DateTime = DateTime.Parse(UpdateDay);
            }
            
            dateInputDate.Value = DateTime.Parse(InputDate);
            checkLastOption.Checked = bool.Parse(LastOption);
            checkTechDate.Checked = bool.Parse(TechDate);
            checkJig.Checked = bool.Parse(Jig);
            txtJigType.Text = JigType;
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            if (txtClampTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ClampTime);
            }
            else if (txtProTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ProTime);
            }
            else if (txtToolQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ToolQty);
            }
            else
            {
                if (txtAondNote.Text == "")
                {
                    txtAondNote.Text = "0";
                }
                try
                {
                    float.Parse(txtAondNote.Text);
                    float.Parse(txtClampTime.Text);
                    float.Parse(txtProTime.Text);
                    int.Parse(txtToolQty.Text);

                    if (dateCLUpdateday.EditValue == null)
                    {
                        dateCLUpdateday.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
                    }

                    if (dateUpdateDay.EditValue == null)
                    {
                        dateUpdateDay.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
                    }

                    obj.InputDate = dateInputDate.Value;
                    obj.PartID = cbPartID.SelectedValue.ToString();
                    obj.MachineID = cbMachineID.SelectedValue.ToString();
                    obj.OptionID = cbOptionID.SelectedValue.ToString();
                    obj.JigID = cbJigID.SelectedText;
                    obj.StaffID = cbStaffID.SelectedValue.ToString();
                    obj.ToolQty = int.Parse(txtToolQty.Text);
                    obj.ProTime = float.Parse(txtProTime.Text);
                    obj.ClampTime = float.Parse(txtClampTime.Text);
                    obj.TechDate = checkTechDate.Checked;
                    obj.UpdateDay = dateUpdateDay.DateTime;
                    obj.Note = txtNote.Text;
                    obj.AondNote = float.Parse(txtAondNote.Text);
                    obj.Memo = txtMemo.Text;
                    obj.CLUpdateday = dateCLUpdateday.DateTime;
                    obj.LastOption = checkLastOption.Checked;
                    obj.ID = int.Parse(ID);
                    obj.Jig = checkJig.Checked;
                    obj.JigType = txtJigType.Text;

                    clspartDAO.UpdateOptionData(obj);
                    MessageBox.Show("Đã nhập xong");
                    this.Re_Load(sender, e);
                }
                catch
                {
                    MessageBox.Show("AondNote, ClampTime, ProTime, ToolQty phải là kiểu số");
                }
            }
        }
    }
}